package com.example.linxilite.screen

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.example.linxilite.network.ChatWebSocketClient
import kotlinx.coroutines.launch

@Composable
fun ChatScreen(username: String?,navController: NavController){
    val messages = remember { mutableStateListOf<String>() }
    var inputText by remember { mutableStateOf("") }
    val coroutineScope = rememberCoroutineScope()

    val client = remember {
        ChatWebSocketClient { msg ->
            coroutineScope.launch {
                messages.add("对方: $msg")
            }
        }
    }

    LaunchedEffect(Unit) {
        client.connect()
    }

    DisposableEffect(Unit) {
        onDispose {
            client.close()
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(12.dp)
    ) {
        Text("正在与 $username 聊天中...", fontSize = 20.sp)
        Spacer(Modifier.height(12.dp))

        LazyColumn(
            modifier = Modifier
                .weight(1f)
                .fillMaxWidth(),
            reverseLayout = true
        ) {
            items(messages.reversed()) { msg ->
                Text(
                    msg,
                    modifier = Modifier
                        .padding(8.dp)
                        .background(
                            if (msg.startsWith("我")) Color(0xFFD2F8D2) else Color(0xFFF1F1F1),
                            shape = RoundedCornerShape(12.dp)
                        )
                        .padding(12.dp)
                )
            }
        }

        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically
        ) {
            OutlinedTextField(
                value = inputText,
                onValueChange = { inputText = it },
                modifier = Modifier.weight(1f),
                placeholder = { Text("输入消息...") }
            )

            Spacer(Modifier.width(8.dp))

            Button(onClick = {
                if (inputText.isNotBlank()) {
                    client.sendMessage(inputText)
                    messages.add("我: $inputText")
                    inputText = ""
                }
            }) {
                Text("发送")
            }
        }
    }


}